import BasePanel from "./BasePanel";
import CommonConstant from "../../constant/CommonConstant";
import LanguageManager from "../../constant/LanguageManager";
import EnumData from "../../Enum/EnumData";
import SelectLevelUi from "../UI/GamePanel/SelectLevelUi";
import { utils } from "../../utils/CommonUtil";

export default class SelectLevelPanel extends BasePanel {
    constructor(id: number) {
        super();
        this.m_nameID = id;

        this.InitFui();
        this.mowner = this.m_fui.displayObject;
        this.mowner.alpha = 0;
        Laya.Tween.to(this.mowner, { alpha: 1 }, 500);
        // this.InitData();
    }

    private InitFui() {

        this.m_fui = window.gameFacade.ResourceMng.
            getMainCom(CommonConstant._fuiSelectLevelPanelPath.Package, "SelectLevelPanel", "SelectLevelPanel");

        let bg = this.m_fui.getChild("bg").asImage;

        let ratio = Laya.Browser.clientWidth / Laya.stage.width; ///  xxx/750 算出相对比例
        let h = Laya.Browser.clientHeight / ratio;
        let ratioH = h / Laya.stage.height;
        let targetW = Laya.stage.width * ratioH;

        bg.height = h;
        bg.width = targetW;

        let titleText = this.m_fui.getChild("title").asLabel;
        titleText.text = window.gameFacade.ConfigMng.GetLanguageByKey(LanguageManager.STORY);

        let backBtn = this.m_fui.getChild("backBtn").asButton;
        backBtn.onClick(this, this.OnBackbtnClick)

        let selectLevelList = this.m_fui.getChild("selectLevelList").asList;
        selectLevelList.removeChildrenToPool();
        selectLevelList.foldInvisibleItems = true;

        let allLevelDatas = window.gameFacade.ConfigMng.allLevelDataList;
        for (let index = 0; index < allLevelDatas.length; index++) {
            const levelData = allLevelDatas[index];
            let levleSelctItemCom: fairygui.GComponent = selectLevelList.addItemFromPool().asCom;
            /** 根据获得的道具 生成对应的道具ui*/
            let selectLevelUi: SelectLevelUi = utils.CommonUtils.CustomAddComponent(levleSelctItemCom.displayObject, SelectLevelUi) as SelectLevelUi;
            selectLevelUi.SetData(levleSelctItemCom, levelData, this);
        }

        super.Show();
    }

    private OnBackbtnClick() {
        window.gameFacade.SceneMng.close(EnumData.EnumPanelType.SelectLevelPanel);
    }
}